//
//  ShareCVCell.m
//  MMLOOK
//
//  Created by dllo on 15/9/28.
//  Copyright © 2015年 武俊娥. All rights reserved.
//

#import "ShareCVCell.h"
#import "ShareCostumView.h"
#import "ShareLayout.h"
#import "ShareCellCostum.h"

@interface ShareCVCell ()<UICollectionViewDataSource, UICollectionViewDelegate>

@property (nonatomic, retain) UICollectionView *collection;
@property (nonatomic, retain) ShareLayout *layout;

@end

@implementation ShareCVCell

- (void)dealloc
{
    Block_release(_sendDic);
    [_arr release];
    [_collection release];
    [_layout release];
    [super dealloc];
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        [self creatCollection];
    }
    return self;
}

- (void)applyLayoutAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes
{
    [super applyLayoutAttributes:layoutAttributes];
    
    self.collection.frame = CGRectMake(0, 0, self.contentView.frame.size.width, self.contentView.frame.size.height);
}

- (void)setArr:(NSMutableArray *)arr
{
    if (_arr != arr) {
        
        [_arr release];
        _arr = [arr retain];
    }
    
    if (_arr.count == 0) {
        
        [self.delegate readLocalData:self];
        [self.delegate beginRefresh:self];
    } else {
        
        self.layout.layoutArray = [NSMutableArray arrayWithArray:_arr];
    }
    
    [_collection reloadData];
}

- (void)creatCollection
{
    self.layout = [[[ShareLayout alloc] init] autorelease];
    _layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    _layout.W = self.frame.size.width;
    _layout.sectionInset = UIEdgeInsetsMake(15 * OffWidth, 15 * OffHeight, 15 * OffWidth, 15 * OffHeight);
    _layout.minimumInteritemSpacing = 15 * OffHeight;
    _layout.minimumLineSpacing = 15 * OffWidth;
    
    self.collection = [[[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.contentView.frame.size.width, self.contentView.frame.size.height) collectionViewLayout:_layout] autorelease];
    _collection.delegate = self;
    _collection.dataSource = self;
    [_collection registerClass:[ShareCellCostum class] forCellWithReuseIdentifier:@"share"];
    [self.contentView addSubview:_collection];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _arr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    ShareCellCostum *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"share" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor orangeColor];
    NSMutableDictionary *dic = [self.arr objectAtIndex:indexPath.row];
    
    [cell sendDic:dic];
    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSMutableDictionary *dic = [_arr objectAtIndex:indexPath.item];
    
    self.sendDic(dic);
}

- (void)sendDicWithBlock:(void (^)(NSMutableDictionary *))block
{
    self.sendDic = block;
}

@end
